package com.lanyou.esb.util;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.security.MessageDigest;
import javax.servlet.http.Part;



public class FileUploadCilent {

	

	public static boolean uploadBundleBySocket(String ip, String port, String fileName, Part file) {
		Socket client = null;
		OutputStream os = null;
		InputStream is = null;
		/*InputStream is1 = null;*/
		InputStream fis = null;
		InputStream fis1 = null;
		try {
			client = new Socket(ip, Integer.parseInt(port));
			os = client.getOutputStream();
			is = client.getInputStream();
			
			System.out.println("send's file name:" + fileName);

			fis = file.getInputStream();

			fis1 = file.getInputStream();
			// 1、发送文件名
			MessageDigest md = MessageDigest.getInstance("MD5");
			// md.digest() 该函数返回值为存放哈希值结果的byte数组

			byte[] b = new byte[(int) file.getSize()];
			fis.read(b);
			byte[] md5b = md.digest(b);
			System.out.println("文件长度: " + file.getSize());
			
			StringBuilder ret = new StringBuilder(md5b.length << 1);
			for (int i = 0; i < md5b.length; i++) {
				ret.append(Character.forDigit((md5b[i] >> 4) & 0xf, 16));
				ret.append(Character.forDigit(md5b[i] & 0xf, 16));
			}
			System.out.println(ret.toString());
			String str = fileName + ":" + ret.toString() + ":" + file.getSize();
			os.write(str.getBytes("utf-8"));
			
			byte[] serverStr1 = new byte[512];
			//从服务端的输出流中获取返回值，会阻塞
			is.read(serverStr1);
			String str2 = new String(serverStr1);
			if (str2.trim().equals("fileOk")) {
				
			}
			
			int length = 0;
			byte[] b2 = new byte[1024];
			while ((length = fis1.read(b2)) != -1) {
				// 2、把文件写入socket输出流
				os.write(b2, 0, length);
			}
			os.flush();
			
			byte[] serverStr = new byte[512];
			//从服务端的输出流中获取返回值，会阻塞
			is.read(serverStr);
			System.out.println("服务端返回的值：" + new String(serverStr));
			
			String str1 = new String(serverStr);
			if (str1.trim().equals("ok")) {
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			return false;
		} finally {
			try {
				os.close();
				fis.close();
				fis1.close();
				is.close();
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	
	public static boolean uploadBundleBySocketForWsdl(String ip, String port, int size, String fileName, InputStream in) {
		Socket client = null;
		OutputStream os = null;
		InputStream is = null;
		try {
			client = new Socket(ip, Integer.parseInt(port));
			os = client.getOutputStream();
			is = client.getInputStream();
			
			System.out.println("send's file name:" + fileName);

			// 1、发送文件名
			MessageDigest md = MessageDigest.getInstance("MD5");
			// md.digest() 该函数返回值为存放哈希值结果的byte数组

			byte[] b = new byte[size];
			in.read(b, 0, size);
			byte[] md5b = md.digest(b);
			System.out.println("文件长度: " + size);
			
			StringBuilder ret = new StringBuilder(md5b.length << 1);
			for (int i = 0; i < md5b.length; i++) {
				ret.append(Character.forDigit((md5b[i] >> 4) & 0xf, 16));
				ret.append(Character.forDigit(md5b[i] & 0xf, 16));
			}
			System.out.println(ret.toString());
			String str = "temp/" + fileName + ":" + ret.toString() + ":" + size;
			os.write(str.getBytes("utf-8"));
			
			byte[] serverStr1 = new byte[512];
			//从服务端的输出流中获取返回值，会阻塞
			is.read(serverStr1);
			String str2 = new String(serverStr1);
			if (str2.trim().equals("fileOk")) {
				
			}
			
			os.write(b, 0, size);
			os.flush();
			
			byte[] serverStr = new byte[512];
			//从服务端的输出流中获取返回值，会阻塞
			is.read(serverStr);
			System.out.println("服务端返回的值：" + new String(serverStr));
			
			String str1 = new String(serverStr);
			if (str1.trim().equals("ok")) {
				return true;
			}else{
				return false;
			}
		} catch (Exception e) {
			return false;
		} finally {
			try {
				in.close();
				os.close();
				is.close();
				client.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
   

	
	/**
	 * @param args
	 */
	/*public static void main(String[] args) {
		// TODO Auto-generated method stub
		Socket client = null;
		try {
			client = new Socket("172.26.142.111", 8888);
			OutputStream os = client.getOutputStream();
			InputStream is = client.getInputStream();
			String filepath = "d:\\apache-tomcat-8.0.21-windows-x64.zip";

			File file = new File(filepath);
			String filename = file.getName();
			System.out.println("send's file name:" + filename);

			InputStream fis = new FileInputStream(file);

			InputStream fis1 = new FileInputStream(file);
			// 1、发送文件名
			MessageDigest md = MessageDigest.getInstance("MD5");
			// md.digest() 该函数返回值为存放哈希值结果的byte数组

			byte[] b = new byte[(int) file.length()];
			fis.read(b);
			byte[] md5b = md.digest(b);
			System.out.println("文件长度: " + file.length());
			StringBuilder ret = new StringBuilder(md5b.length << 1);
			for (int i = 0; i < md5b.length; i++) {
				ret.append(Character.forDigit((md5b[i] >> 4) & 0xf, 16));
				ret.append(Character.forDigit(md5b[i] & 0xf, 16));
			}
			System.out.println(ret.toString());
			String str = filename + ":" + ret.toString() + ":" + file.length();
			os.write(str.getBytes());
			int length = 0;
			byte[] b2 = new byte[(int) file.length()];
			while ((length = fis1.read(b2)) != -1) {
				// 2、把文件写入socket输出流
				// System.out.println(new String(b));
				System.out.println(length);
				os.write(b2, 0, length);
			}
			os.flush();
			// fis1.read(b);
			// os.write(b);
			byte[] serverStr = new byte[1024];

			is.read(serverStr);
			System.out.println("sdf" + new String(serverStr));
			String str1 = new String(serverStr);
			if (str1.trim().equals("ok")) {
				String s = str1;
			}
			os.close();
			fis.close();
			fis1.close();
			is.close();
			System.out.println("send over");
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

			try {
				client.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}*/
}
